package com.ruoyi.web.controller.website;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.module.domain.Evaluate;
import com.ruoyi.module.domain.Order;
import com.ruoyi.module.domain.Points;
import com.ruoyi.module.service.IOrderService;
import com.ruoyi.module.service.IPointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 订单Controller
 *
 * @author fubojin
 * @date 2024-12-06
 */
@RestController
@RequestMapping("/website/order")
public class WebOrderController extends BaseController {
    @Autowired
    private IOrderService orderService;

    @Autowired
    private IPointsService pointsService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Order order) {
        startPage();
        order.setOrderType(3);
        List<Order> list = orderService.selectOrderWebList(order);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Order order) {
        List<Order> list = orderService.selectOrderList(order);
        ExcelUtil<Order> util = new ExcelUtil<Order>(Order.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(orderService.selectOrderById(id));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Order order) {
        Order order1 = orderService.insertOrder(order);
        //添加积分明细
        if (order.getOrderType() == 5) {
            Points points = new Points();
            points.setType(5);
            points.setOrderId(order.getId());
            points.setUserId(order.getUserId());
            points.setPointsQuantity(-(order.getOrderTotalMoney().intValue()));
            pointsService.insertPoints(points);
        }
        return success(order1);
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Order order) {
        return toAjax(orderService.updateOrder(order));
    }

    /**
     * 删除订单
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(orderService.deleteOrderByIds(ids));
    }


    /**
     * 收货
     */
    @PostMapping(value = "setSignForReceipt/{id}")
    public AjaxResult setSignForReceipt(@PathVariable("id") Long id){
        orderService.setSignForReceipt(id);
        return success();
    }

}
